import axios from 'axios'
export function delete_cluster_resource(name,url){
    let cluster = localStorage.getItem('currentCluster')
    let headers = {"cluster_name": cluster }
    let data = JSON.stringify({"name":name})
    let method='post'
    if(cluster){
        axios({
            url:url,headers: headers,data:data,method:method
        }).then( (response) => {
            let info = JSON.stringify(response.data)
            if(info.indexOf('ok') != -1) {
                alert('删除'+name+'成功')
                return true
            }else {
                alert("删除失败:",info)
                return false
            }
        })
    }
}
var forbidden_delete_namespace_list = ['ingress-nginx','istio-system','kube-node-leas','kube-public','monitoring','kube-system','kube-ops','cattle-prometheus','cattle-system','security-scan']
export function delete_namespace_resource(namespace,name,url){
    if (forbidden_delete_namespace_list.indexOf(namespace) != -1){
        alert("核心系统资源不允许删除")
        return
    }
    let cluster = localStorage.getItem('currentCluster')
    let headers = {"cluster_name": cluster }
    let data = JSON.stringify({"namespace":namespace,"name":name})
    console.log("data: ",data)
    let method='post'
    if(cluster){
        axios({
            url:url,headers: headers,data:data,method:method
        }).then( (response) => {
            let info = JSON.stringify(response.data)
            if(info.indexOf('ok') != -1) {
                alert('删除'+name+'成功')
                return true
            }else {
                alert("删除失败:",info)
                return false
            }
        })
    }
}
// 批量删除
export function multi_delete_namespace_resource(namespace,name_list,url){
    if (forbidden_delete_namespace_list.indexOf(namespace) != -1){
        alert("核心系统资源不允许删除")
        return
    }
    let cluster = localStorage.getItem('currentCluster')
    let headers = {"cluster_name": cluster }
    let data = JSON.stringify({"namespace":namespace,"name_list":name_list})
    let method='post'
    if(cluster){
        axios({
            url:url,headers: headers,data:data,method:method
        }).then( (response) => {
            let info = JSON.stringify(response.data)
            if(info.indexOf('ok') != -1) {
                alert('批量删除'+name_list+'成功')
                return true
            }else {
                alert("删除失败:",info)
                return false
            }
        })
    }
}

export function delete_ns(name,url){
    if (forbidden_delete_namespace_list.indexOf(name) != -1){
        alert("核心系统资源不允许删除")
        return
    }
    let cluster = localStorage.getItem('currentCluster')
    let headers = {"cluster_name": cluster }
    let data = JSON.stringify({"name":name})
    let method='post'
    if(cluster){
        axios({
            url:url,headers: headers,data:data,method:method
        }).then( (response) => {
            let info = JSON.stringify(response.data)
            if(info.indexOf('ok') != -1) {
                alert('删除'+name+'成功')
                return true
            }else {
                alert("删除失败:",info)
                return false
            }
        })
    }
}

// 分页页码改变


// 显示json超长内容，在不同行显示位置不一样
export function get_placement(params){
    let placement = 'bottom-start'
    if(params.row._index<4){
        placement = 'bottom-start'
    }else if(params.row._index>4 && params.row._index < 7){
        placement = 'right'
    }else{
        placement = 'top-start'
    }  
    return placement
}

export function show_simple_page(obj,response) {
    let _this = obj
    _this.total_list = response.data
    _this.total = response.data.length
    if(_this.total < _this.pageSize) {
        _this.show_list = _this.total_list
    }else {
        // 修改改数据之后显示回到第一页的bug，改为停留在当前页
        let currentPage = _this.$refs.page.currentPage
        let _start = (currentPage-1) * _this.pageSize
        let _end = currentPage * _this.pageSize
        _this.show_list = _this.total_list.slice(_start,_end)
    }
}

// export  function changePage(index) {
//     let _start = (index -1) * this.pageSize
//     let _end = index * this.pageSize
//     this.show_list = this.total_list.slice(_start,_end)
// },


export function utcToBeijing(utc_datetime) { // utc转北京时间  例： 2020-05-08T04:25:44.000Z  -> 2020/05/08 12:25
    // 转为正常的时间格式 年-月-日 时:分:秒
    var T_pos = utc_datetime.indexOf('T');
    var Z_pos = utc_datetime.indexOf('Z');
    var year_month_day = utc_datetime.substr(0,T_pos);
    var hour_minute_second = utc_datetime.substr(T_pos+1,Z_pos-T_pos-1);
    var new_datetime = year_month_day+" "+hour_minute_second; // 2017-03-31 08:02:06

    // 处理成为时间戳
    timestamp = new Date(Date.parse(new_datetime));
    timestamp = timestamp.getTime();
    timestamp = timestamp/1000;

    // 增加8个小时，北京时间比utc时间多八个时区
    var timestamp = timestamp+8*60*60;

    // 时间戳转为时间
    var beijing_datetime = new Date(parseInt(timestamp) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
    return beijing_datetime; // 2017-03-31 16:02:06
}
export const colors = {
    'red': '#ed4014',
    'green': '#19be6b',
    'blue': '#2db7f5',
    'yellow': '#ff9900',
    'gray': '#515a6e',
    'black': '#17233d',
}

//将" '1','2' "转成Number类型数组
export function str_to_num_array(str) {
    if (typeof (str) != 'string') {
        console.log("输入不是字符串")
        return []
    }
    let arr = str.split(",")
    if (arr.length == 0) {
        return []
    } else {
        return arr.map(Number)
    }
}

export function str_to_str_array(str) {
    if (typeof (str) != 'string') {
        console.log("输入不是字符串")
        return []
    }
    let arr = str.split(",")
    if (arr.length == 0) {
        return []
    } else {
        return arr.map(String)
    }
}

//将数组转成字符串
export function array_to_str(arr) {
    console.log(typeof (arr))
    if (typeof (arr) != 'object') {
        console.log("输入不是数组")
        return ""
    }
    let str = arr.join(",")
    return str
}


//前端将UTC时间格式化 垃圾golang   "2021-12-13T17:30:00+08:00"
export function formatUtcTime(v) {
    let date = new Date(v);
    let y = date.getFullYear()
    let m = (date.getMonth() + 1)
    let d = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) 
    let H = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) 
    let M = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) 
    let S = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()) 
    return y+"-"+m+"-"+d+" "+H+":"+M+":"+S
}